package com.bangyu.monitor.modular.system.service;

import com.bangyu.monitor.core.common.page.LayuiPageFactory;
import com.bangyu.monitor.modular.system.entity.Risk;
import com.bangyu.monitor.modular.system.entity.RiskCount;
import com.bangyu.monitor.modular.system.mapper.RiskMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Auther: zhangxue
 * @Date: 19/4/18 22:33
 * @Description:
 */
@Service
@Slf4j
public class RiskService extends ServiceImpl<RiskMapper, Risk> {

//    private static final ExecutorService executor = Executors.newFixedThreadPool(2);
    public void batchInsert(List<Risk> risks){
        if(CollectionUtils.isNotEmpty(risks)){
            log.info("报警数据入库{}条,警报详情 {}",risks.size(),riskLog(risks));
            this.baseMapper.batchInsert(risks);
        }
    }
//    public void asychInsert(List<Risk> risk){
//        executor.execute(new Runnable() {
//            @Override public void run() {
//                batchInsert(risk);
//            }
//        });
//    }

    private String riskLog(List<Risk> risks){
        StringBuilder sb = new StringBuilder();
        if(CollectionUtils.isNotEmpty(risks)){
           for(Risk risk:risks){
               sb.append(risk.toString()).append("\n\r");
           }
        }
        return sb.toString();
    }

    /**
     * 根据桥梁id查询桥梁的报警信息
     * @param bridgeIds
     * @return
     */
    public Page<Risk> pageQueryByBridges(List<Long> bridgeIds,Integer riskLevel,Integer riskType){
        Page page = LayuiPageFactory.defaultPage();
        return this.baseMapper.pageQueryByBridges(page,bridgeIds,riskLevel,riskType);
    }

    /**
     * 报警的桥梁数量
     * @param startDate
     * @param endDate
     * @return
     */
    public Integer bridgeCountOfRisk(List<Long> bridgeIds,Integer riskLevel,Integer startDate,Integer endDate){
        return this.baseMapper.bridgeCountOfRisk(bridgeIds,riskLevel,startDate,endDate);
    }

    /**
     * 按风险等级和风险类别获取梁的报警次数
     * @param beamNo
     * @param startDate
     * @param endDate
     * @return
     */
    public List<RiskCount> riskCountByLevelAndType(String beamNo,Integer startDate,Integer endDate){
        return this.baseMapper.riskCountByLevelAndType(beamNo,startDate,endDate);
    }


}
